package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.base.BaseController;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.DriverExcution;
import com.fortune.dto.excution.SalesManExcution;
import com.fortune.dto.excution.SmsExcution;
import com.fortune.dto.in.cOrderLogin.*;
import com.fortune.dto.in.salesMan.SalesManChangePasswordDto;
import com.fortune.dto.in.salesMan.SalesManVeriPasswordDto;
import com.fortune.dto.in.sms.SmsReSetPassWordDto;
import com.fortune.entity.PhoneAndPassword;
import com.fortune.enums.DriverEnum;
import com.fortune.enums.SalesManEnum;
import com.fortune.enums.SmsEnum;
import com.fortune.service.COrderLoginService;
import com.fortune.service.SmsService;
import com.fortune.util.RandomCodeUtil;
import com.fortune.util.TokenUtil;
import com.fortune.vo.COrderLogin.COrderLoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



@Api(value = "wechat/cOrderLogin", description = "客户账户", tags = {"微信小程序（客户）：账户相关"})
@RestController
@RequestMapping("wechat/cOrderLogin")
@Validated
public class COrderLoginController extends BaseController{
	@Autowired
	private RedisClient redisClient;

	@Autowired
	private COrderLoginService cOrderLoginService;

	@Autowired
	private SmsService smsService;

	@ApiOperation(value = "客户登录", nickname = "cOrderLogin", notes = "", tags = {"@郭阳"})
	@PostMapping(value = "login",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<COrderLoginInfo> login(@Validated @RequestBody COrderLoginDto cOrderLoginDto) {
		String phone = cOrderLoginDto.getPhone();
		String password = cOrderLoginDto.getPassword();
		//验证手机号是否存在  PhoneAndPassword这里有厂商和经销商id
		PhoneAndPassword finPhoneIsExist = cOrderLoginService.finPhoneIsExist(phone);
		if(finPhoneIsExist == null){
			return new SalesManExcution<COrderLoginInfo>(null, SalesManEnum.ACCOUNT_IS_NOT_EXIST);
		}
		//验证密码是否正确
		String passwords = finPhoneIsExist.getPassword();
		if(!password.equals(passwords)){
			return new SalesManExcution<COrderLoginInfo>(null, SalesManEnum.PASSWORD_ERROR);
		}
		//验证账号是否被禁用
		String status = finPhoneIsExist.getStatus();
		if(status.equals("NO")){
			return new SalesManExcution<COrderLoginInfo>(null, SalesManEnum.ACCOUNT_IS_DISABLED);
		}
		String id = finPhoneIsExist.getId();
		//获取随机唯一字符串
		String token = TokenUtil.genToken();
		String loginToken = id + "," + "aaa" + "," + phone;
		//key CacheKeys+随机字符串 value 手机号 存到redis里
		redisClient.save(loginToken, 60*24, CacheKeys.SALES_MAN_LOGIN_TOKEN, token);
		COrderLoginInfo findCOrderLoginInfo = cOrderLoginService.findCOrderLoginInfo(phone);
		findCOrderLoginInfo.setToken(token);
		return new SalesManExcution<COrderLoginInfo>(findCOrderLoginInfo, SalesManEnum.LOGIN_SUCCESS);
	}

	@ApiOperation(value = "客户重置密码", nickname = "cOrderResetPassword", notes = "", tags={ "@郭阳" })
	@PostMapping(value = "password/reset",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(@Validated @RequestBody COrderLoginResetPasswordDto cOrderLoginResetPasswordDto) {
	/*	String userId = getUserId();
		String phone = getPhone();
		String codeInfor = "123456";
		String code = cOrderLoginResetPasswordDto.getCode();
		if(!codeInfor.equals(code)){
			return new SalesManExcution<Void>(null, SalesManEnum.CODE_ERROR);
		}*/
		String phone = cOrderLoginResetPasswordDto.getPhone();
		String password = cOrderLoginResetPasswordDto.getPassword();
		int editResetPassword = cOrderLoginService.editResetPassword(phone,password);
		if(editResetPassword < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_SUCCESS);
	}

	@ApiOperation(value = "客户修改密码", nickname = "cOrderChangePassword", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "password/change",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> changePassword(@Validated @RequestBody SalesManChangePasswordDto salesManChangePasswordDto) {
		String userId = getUserId();
		String phone = getPhone();
		String oldPassword = salesManChangePasswordDto.getOldPassword();
		/*根据手机号查询密码*/
		String findPasswordByPhone = cOrderLoginService.findPasswordByPhone(userId ,phone);
		if(!findPasswordByPhone.equals(oldPassword)){
			return new SalesManExcution<Void>(null, SalesManEnum.PASSWORD_ERROR);
		}
		String newpassword = salesManChangePasswordDto.getNewPassword();
		int editPasswordByPhone = cOrderLoginService.editPasswordByPhone(userId, phone, newpassword);
		if(editPasswordByPhone < 0){
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}

	@ApiOperation(value = "客户修改姓名", nickname = "cOrderEditName", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "editName",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> editName(@Validated @RequestBody COrderEditNameDto cOrderEditNameDto) {
		String userId = getUserId();
		String name = cOrderEditNameDto.getName();
		int editCOrderName = cOrderLoginService.editCOrderName(userId, name);
		if(editCOrderName < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.EDIT_NAME_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.EDIT_NAME_SUCCESS);
	}

	@ApiOperation(value = "客户验证手机号", nickname = "cOrderVeriPhone", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "veriPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriPhone(@Validated @RequestBody COrderEditPhoneDto cOrderEditPhoneDto) {
		String phone = cOrderEditPhoneDto.getPhone();
		String code = cOrderEditPhoneDto.getCode();
		String codes = redisClient.get(CacheKeys.EMAIL_SEND_CODE,phone);
		if (!code.equals(codes)) {
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}

	@ApiOperation(value = "客户修改手机号", nickname = "cOrderVeriNewPhone", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "veriNewPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriNewPhone(@Validated @RequestBody COrderEditPhoneDto cOrderEditPhoneDto) {
		String userId = getUserId();
		String phone = cOrderEditPhoneDto.getPhone();
		String code = cOrderEditPhoneDto.getCode();
		String codes = redisClient.get(CacheKeys.EMAIL_SEND_CODE,phone);
		if (!code.equals(codes)) {
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}
		int editNewPhone = cOrderLoginService.editNewPhone(phone, userId);
		if(editNewPhone < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PHONE_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}

	@ApiOperation(value = "客户修改店铺地址", nickname = "cOrderVeriAddress", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "cOrderVeriAddress",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> cOrderVeriAddress(@Validated @RequestBody COrderVeriAddressDto cOrderVeriAddressDto) {
		String userId = getUserId();
		int editAddress = cOrderLoginService.editAddress(cOrderVeriAddressDto, userId);
		if(editAddress < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.UPDATE_ADDRESS_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.UPDATE_ADDRESS_SUCCESS);
	}

	@ApiOperation(value = "客户修改店铺名称", nickname = "cOrderEditStoreName", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "editStoreName",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> editStoreName(@Validated @RequestBody COrderEditNameDto cOrderEditNameDto) {
		String userId = getUserId();
		String storeName = cOrderEditNameDto.getName();
		int editStoreName = cOrderLoginService.editStoreName(userId, storeName);
		if(editStoreName < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.UPDATE_STORE_NAME_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.UPDATE_STORE_NAME_SUCCESS);
	}

	@ApiOperation(value = "客户验证手机号", nickname = "clientVeriPassword", notes = "", tags={ "@郭阳" })
	@PostMapping(value = "password/veri",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriPassword(@Validated @RequestBody SalesManVeriPasswordDto salesManVeriPasswordDto) {
		String phone = salesManVeriPasswordDto.getPhone();
		String code = salesManVeriPasswordDto.getCode();
		String codes = redisClient.get(CacheKeys.EMAIL_SEND_WECHAT_CODE,phone);
		if (!code.equals(codes)) {
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}

	@ApiOperation(value = "发送注册账号短信", nickname = "sendRegisteredPasswordSms", notes = "", tags={ "@郭阳" })
	@PostMapping(value = "code/registeredPassword",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> sendRegisteredCode(@Validated @RequestBody SmsReSetPassWordDto reSetPassWordDto) {
		String phone = reSetPassWordDto.getPhone();
		int code = RandomCodeUtil.get6RandomCode();
		boolean flag = smsService.sendRegisteredCode(phone, String.valueOf(code));
		if (!flag) {
			return new SmsExcution<Void>(null,SmsEnum.FALSE);
		}
		redisClient.save(String.valueOf(code), 5, CacheKeys.EMAIL_SEND_WECHAT_CODE, phone);
		return new SmsExcution<Void>(null,SmsEnum.SUCCESS);
	}
}
